using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class main_script : MonoBehaviour
{
    public static int[,] chess_arr = new int[4,4];
    public static bool remake_flag = false;
    public static string ifwin_msg = "";
    public static int first_one = -1;
    public static int now_who = 1;
    public static bool mode_flag = true;
    public static int[] game_score = {0,0};
    
    public static bool put(int pos,int side){
        if(pos>8||pos<0) return false;
        int x=pos/3,y=pos%3;
        Debug.Log(x.ToString()+"|"+y.ToString());
        if(chess_arr[x,y]==0){
            chess_arr[x,y]=side;
            // color
            GameObject.Find("main_runner").GetComponent<main_script>().color_chess(pos,side);
            // check win
            int s1=0,s2=0,s3=0,s4=0,s5=0;
            for(int i=0;i<3;i++){
                s3+=chess_arr[i,i];
                s4+=chess_arr[i,2-i];
                s1=0;s2=0;
                for(int j=0;j<3;j++){
                    s5+=(chess_arr[i,j]==0)? 0:1;
                    s1+=chess_arr[i,j];
                    s2+=chess_arr[j,i];
                    if(s1==3||s2==3||s3==3||s4==3){
                        Debug.Log(s1.ToString()+s2.ToString()+s3.ToString()+s4.ToString());
                        ifwin_msg = "红方胜利";
                        remake_flag=true;
                        game_score[0]++;
                        return true;
                    }
                    if(s1==-3||s2==-3||s3==-3||s4==-3){
                        ifwin_msg = "蓝方胜利";
                        remake_flag=true;
                        game_score[1]++;
                        return true;
                    }
                    if(s5==9){
                        ifwin_msg = "这是平局";
                        remake_flag=true;
                        return true;
                    }
                }
            }
            if(mode_flag==true&&side == 1)
                GameObject.Find("main_runner").GetComponent<main_script>().bot_put(s5);
            now_who = -now_who;

            return true;
        }
        return false;
    }
    public static void remake(int mode){
        if(mode==1){
            mode_flag= !mode_flag;
            first_one=-1;
            game_score[0]=0;
            game_score[1]=0;
        }
        Debug.Log("remake");
        remake_flag = false;
        ifwin_msg = "";
        first_one = - (first_one);
        now_who=first_one;
        for(int i=0;i<3;++i)
        for(int j=0;j<3;++j){
            chess_arr[i,j]=0;
            GameObject.Find("Cube ("+(i*3+j+1).ToString()+")").GetComponent<MeshRenderer>().material = Resources.Load<Material>("Materials/simple");
        }
        if(mode_flag==true&&first_one==-1){
            GameObject.Find("main_runner").GetComponent<main_script>().bot_put(0);
        }
    }

    public Material mat_o;
    public Material mat_x;

    bool color_chess(int pos,int side){
        Debug.Log("color |"+ pos.ToString()+" | "+side.ToString());
        if(side==1)  GameObject.Find("Cube ("+(pos+1).ToString()+")").GetComponent<MeshRenderer>().material = Resources.Load<Material>("Materials/oc");
        if(side==-1)  GameObject.Find("Cube ("+(pos+1).ToString()+")").GetComponent<MeshRenderer>().material = Resources.Load<Material>("Materials/xc");
        return false;
    }
    void bot_put(int step){
        int r=-1;

        if(step==0){
            int[] rd = {0,2,6,8,4};
            r=  rd[Random.Range(0,5)];
        }
        if(step==1){
            if(r==-1&&chess_arr[1,1]==0)    r=4;
        }
        if(step==3){
            int[] rd = {1,3,5,7};
            if((chess_arr[0,0]==1&&chess_arr[2,2]==1)||(chess_arr[0,2]==1&&chess_arr[2,0]==1))
                r=rd[Random.Range(0,4)];
        }

        int s1,s2,s3=0,s4=0;
        for(int i=0;i<3;i++){
            s1=0;s2=0;
            s3+=chess_arr[i,i];
            s4+=chess_arr[i,2-i];
            for(int j=0;j<3;j++){
                s1+=chess_arr[i,j];
                s2+=chess_arr[j,i];
            }
            if(s1==2||s1==-2){
                for(int j=0;j<3;j++)
                    if(chess_arr[i,j]==0)   r=i*3+j;
                if(s1==-2) break;
            }
            if(s2==2||s2==-2){
                for(int j=0;j<3;j++)
                    if(chess_arr[j,i]==0)   r=j*3+i;
                if(s2==-2)  break;
            }
        }
        if(s3==-2)
            for(int j=0;j<3;++j)
                if(chess_arr[j,j]==0)  r=j*3+j;
        if(s4==-2)
            for(int j=0;j<3;++j)
                if(chess_arr[j,2-j]==0)  r=j*3+2-j;
        if(r==-1){
            if(s3==2)
                for(int j=0;j<3;++j)
                    if(chess_arr[j,j]==0)   r=j*3+j;
            if(s4==2)
                for(int j=0;j<3;++j)
                    if(chess_arr[j,2-j]==0) r=j*3+2-j;
        }
        if(r==-1){
            if(chess_arr[1,1]==-1){
                if(chess_arr[0,0]==1&&chess_arr[2,2]==0)   r=8;
                if(chess_arr[2,0]==1&&chess_arr[0,2]==0)   r=2;
                if(chess_arr[0,2]==1&&chess_arr[2,0]==0)   r=6;
                if(chess_arr[2,2]==1&&chess_arr[0,0]==0)   r=0;
            }
            if(chess_arr[0,0]==-1&&chess_arr[2,2]==0)   r=8;
            if(chess_arr[2,0]==-1&&chess_arr[0,2]==0)   r=2;
            if(chess_arr[0,2]==-1&&chess_arr[2,0]==0)   r=6;
            if(chess_arr[2,2]==-1&&chess_arr[0,0]==0)   r=0;
        }
        if(r==-1&&chess_arr[1,1]==0)    r=4;
        if(r==-1){
            if(main_script.chess_arr[0,0]==0)   r=0;
            if(main_script.chess_arr[0,2]==0)   r=2;
            if(main_script.chess_arr[2,0]==0)   r=6;
            if(main_script.chess_arr[2,2]==0)   r=8;
        }
        if(r==-1){
            for(int i=0;i<9;++i)
            if(chess_arr[i/3,i%3]==0)
                r=i;
        }
        
        
        // int r=Random.Range(0,9);
        // while(true){
        //     if(main_script.put(r,-1)==true) break;
        //     r=Random.Range(0,9);
        // }
        main_script.put(r,-1);
    }

    // Start is called before the first frame update
    void Start()
    {
        Resolution[] resolutions=Screen.resolutions;
        Debug.Log(((resolutions[0].width).ToString()) + ":" + ((resolutions[0].height).ToString()));
        main_script.remake(0);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    
}
